package com.hww.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hww.dao.OrdersMapper;
import com.hww.dao.UserMapper;
import com.hww.entity.Agreement;
import com.hww.entity.Order;
import com.hww.entity.User;
import com.hww.service.Impl.UserServiceImpl;
import com.hww.util.JWTUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import javax.websocket.server.PathParam;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@Api(value = "用户管理")
public class UserController {
  @Resource private UserServiceImpl userService;

  @Autowired private UserMapper userMapper;

  private Agreement agreement = new Agreement();

  @Autowired private OrdersMapper ordersMapper;

  @GetMapping("/test")
  @ResponseBody
  public String Test() {
    return "成功";
  }

  /**
   * 登陆控制器
   *
   * @param user
   * @param session
   * @return
   */
  @SuppressWarnings("rawtypes")

  @PostMapping("/login")
  @ResponseBody
  @ApiOperation(value = "登陆控制器", notes = "前端传递通过ajax传递账号密码")
  public Agreement login_In(
      @ApiParam(value = "用户实体类", required = true) User user, HttpSession session) {
    String sessionId = session.getId();
    agreement = new Agreement();
    String password = userService.queryUser(user);
    System.out.println(password);
    if (password != null) {
      if (user.getPassword().equals(password)) {

        String token = new JWTUtil().createToken(user);
        session.setAttribute(user.getUsername(), token);
        agreement.setData(user.getUsername());
        agreement.setMsg("登陆成功！");
        agreement.setPath("mall/frame.html");
        return agreement;
      } else {
        agreement.setCode(400);
        agreement.setMsg("密码错误！");
        return agreement;
      }
    }
    agreement.setCode(400);
    agreement.setMsg("账户错误！");
    return agreement;
  }


  @PostMapping("/editpass")
  @ResponseBody
  public Map editPassword(HttpServletRequest request) {
    Map<String, Object> map = new HashMap<>();
    String oldpassword = request.getParameter("oldpassword");
    String password = request.getParameter("password");
    String user = request.getParameter("user");
    User user1 = new User();
    user1.setUsername(user);
    user1.setPassword(password);
    String pass = userMapper.selectUser(user1);
    if (oldpassword.equals(pass)) {
      userMapper.updatePassworld(user1);
      map.put("msg", "修改成功");
      map.put("code", "200");

    } else {
      map.put("msg", "修改失败，旧密码错误！");
      map.put("code", "400");
    }
    return map;
  }

  /**
   * 模糊查询
   *
   * @param ordername
   * @return
   */
  @GetMapping("/query/order")
  @ResponseBody
  public List<Map<String, Object>> queryGoods(@RequestParam(value = "ordername") String ordername) {

    List<Map<String, Object>> list =
        ordersMapper.selectMaps(new QueryWrapper<Order>().like("business", ordername));

    return list;
  }

  /**
   * 增加功能
   *
   * @param order
   * @return
   */
  @PostMapping("/order")
  @ResponseBody
  public Agreement increaseOrder(@RequestBody Order order) {
    Integer num = ordersMapper.insert(order);
    Agreement agreement = new Agreement();
    if (num > 0) {
      agreement.setMsg("添加成功！");
    } else {
      agreement.setMsg("添加失败！");
    }
    return agreement;
  }

  /**
   * index页面所有数据
   *
   * @return
   */
  @SuppressWarnings({"rawtypes", "unchecked"})
  @GetMapping("/order")
  @ResponseBody
  public IPage<Order> orderList(HttpSession session, @PathParam("pages") Long pages) {

    QueryWrapper<Order> entityWrapper = new QueryWrapper();

    Page<Order> page = new Page<Order>(pages, 10);

    IPage<Order> selectPage = ordersMapper.selectPage(page, entityWrapper);

    return selectPage;
  }

  /**
   * 根据id删除
   *
   * @param id
   * @return
   */
  @DeleteMapping("/{id}")
  @ResponseBody
  public Integer delOrder(@PathVariable Integer id) {
    System.out.println("id" + id);
    Integer num = userService.cutOrder(id);
    return num;
  }

  /**
   * 修改功能
   *
   * @param order
   * @return
   */
  @PutMapping("/order")
  @ResponseBody
  public Integer editOrder(@RequestBody Order order) {
    Integer num = userMapper.updateOrder(order);
    return num;
  }
}
